package com.aps.apspda.adapter;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aps.apspda.R;
import com.aps.apspda.dialog.GeneralPop;
import com.aps.apspda.entity.InputDataEntity;
import com.aps.apspda.utils.DisplayUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lx
 * @date 2019/1/23
 * @desc
 */

public class CheckInputDataListAdapter extends RecyclerView.Adapter<CheckInputDataListAdapter.ViewHolder> {

    private List<InputDataEntity.InputObject> dataList;

    public CheckInputDataListAdapter() {
        dataList = new ArrayList<>();
    }

    public List<InputDataEntity.InputObject> getDataList() {
        return dataList;
    }

    public void setDataList(List<InputDataEntity.InputObject> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_check_input_list, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final InputDataEntity.InputObject item = dataList.get(position);
        if (item.getDATACHTITLE().length() != 0) {
            holder.tvTips.setVisibility(View.VISIBLE);
            holder.llContent.setVisibility(View.GONE);
            holder.tvTips.setText(item.getDATACHTITLE());
        } else {
            holder.tvTips.setVisibility(View.GONE);
            holder.llContent.setVisibility(View.VISIBLE);
            if (item.getISNULL().equalsIgnoreCase("Y")) {
                holder.tvTitle.setText("*" + item.getDATACNNAME());
                holder.tvTitle.getPaint().setFakeBoldText(true);//加粗
            } else {
                holder.tvTitle.setText(item.getDATACNNAME());
                holder.tvTitle.getPaint().setFakeBoldText(false);//加粗
            }
            LayoutInflater inflater = LayoutInflater.from(holder.tvTips.getContext());
            final View popView = inflater.inflate(R.layout.popupwindow_down_list_select, null);
            RecyclerView lvDownList = popView.findViewById(R.id.lvDownList);
            lvDownList.setLayoutManager(new LinearLayoutManager(holder.tvTips.getContext()));
            lvDownList.setItemAnimator(new DefaultItemAnimator());
            final PopDownAreaListAdapter popDownAreaListAdapter = new PopDownAreaListAdapter(new ArrayList<String>());
            popDownAreaListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            popDownAreaListAdapter.isFirstOnly(false);
            popDownAreaListAdapter.setCurrentSelect(0);
            lvDownList.setAdapter(popDownAreaListAdapter);

            switch (item.getDATATYPE()) {
                case "1":
                    holder.etContent.setVisibility(View.VISIBLE);
                    holder.llCheck.setVisibility(View.GONE);
                    holder.llChoose.setVisibility(View.GONE);
                    if (item.getLANGUAGETYPE().equals("A")) {
                        holder.etContent.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    } else {
                        holder.etContent.setInputType(InputType.TYPE_CLASS_TEXT);
                    }
                    //把Bean与输入框进行绑定
                    holder.etContent.setTag(item);
                    //清除焦点
                    holder.etContent.clearFocus();
//                    holder.etContent.addTextChangedListener(null); //清除上个item的监听，防止oom
                    holder.etContent.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //获得Edittext所在position里面的Bean，并设置数据
                            InputDataEntity.InputObject item = (InputDataEntity.InputObject) holder.etContent.getTag();
                            if (item.getDATAVALUE().toUpperCase().contains("NG")) {
                                item.setDATAVALUE(s.length() > 0 ? "NG;" + s.toString() : "NG;");
                            } else {
                                item.setDATAVALUE(s.length() > 0 ? s.toString() : "");
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });

                    //大部分情况下，Adapter里面有if必须有else
                    if (!TextUtils.isEmpty(item.getDATAVALUE())) {
                        holder.etContent.setText(item.getDATAVALUE());
                    } else {
                        holder.etContent.setText("");
                    }
                    //add by bunny   NA项不可编辑，不可获取焦点
                    if(holder.etContent.getText().toString().equals("NA"))
                    {
                        holder.etContent.setEnabled(false);
                        holder.etContent.setFocusable(false);
                    }
                    //end
                    //add by bunny  点击提交，焦点停留在第一个*未填写内容上
                    if(item.getISFOCUS())
                    {
                       // holder.etContent.setFocusable(true);
                        //holder.etContent.setFocusableInTouchMode(true);
                        holder.etContent.requestFocus();
                    }
                    //end
                    break;
                case "2":
                    holder.etContent.setVisibility(View.GONE);
                    holder.llCheck.setVisibility(View.VISIBLE);
                    holder.llChoose.setVisibility(View.GONE);
                    if (item.getLANGUAGETYPE().equals("A")) {
                        holder.etNGChooseOther.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    } else {
                        holder.etNGChooseOther.setInputType(InputType.TYPE_CLASS_TEXT);
                    }
                    //把Bean与输入框进行绑定
                    holder.etNGChooseOther.setTag(item);
                    //清除焦点
                    holder.etNGChooseOther.clearFocus();
                    holder.etNGChooseOther.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //获得Edittext所在position里面的Bean，并设置数据
                            InputDataEntity.InputObject item = (InputDataEntity.InputObject) holder.etNGChooseOther.getTag();
                            if (item.getDATAVALUE().toUpperCase().contains("NG")) {
                                if (item.getDATAVALUE().contains("其他;")) {
                                    item.setDATAVALUE(s.length() > 0 ? "NG其他;" + s.toString() : "NG其他;");
                                } else {
                                    item.setDATAVALUE(s.length() > 0 ? "NG;" + s.toString() : "NG;");
                                }
                            } else {
                                item.setDATAVALUE(s.length() > 0 ? s.toString() : "");
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });
                    //大部分情况下，Adapter里面有if必须有else
                    if (!TextUtils.isEmpty(item.getDATAVALUE())) {
                        if (item.getDATAVALUE().equals("OK")) {
                            holder.rbOK.setChecked(true);
                            holder.rbNG.setChecked(false);
                            holder.tvNGChoose.setVisibility(View.GONE);
                            holder.etNGChooseOther.setVisibility(View.GONE);
                        } else {
                            if (item.getDATAVALUE().toUpperCase().contains("NG")) {
                                holder.rbOK.setChecked(false);
                                holder.rbNG.setChecked(true);
                                holder.tvNGChoose.setVisibility(View.VISIBLE);
                                if (item.getDATAVALUE().contains("其他")) {
                                    holder.tvNGChoose.setText("其他");
                                    holder.etNGChooseOther.setVisibility(View.VISIBLE);
                                    if (item.getDATAVALUE().equals("NG其他;")) {
                                        holder.etNGChooseOther.setText("");
                                    } else {
                                        holder.etNGChooseOther.setText(item.getDATAVALUE().split(";")[1]);
                                    }

                                } else {
                                    if (item.getDATAVALUE().equals("NG;")) {
                                        holder.tvNGChoose.setText("请选择");
                                    } else {
                                        holder.tvNGChoose.setText(item.getDATAVALUE().split(";")[1]);
                                    }
                                    holder.etNGChooseOther.setVisibility(View.GONE);
                                }
                            } else {
                                holder.rbOK.setChecked(false);
                                holder.rbNG.setChecked(false);
                                holder.tvNGChoose.setVisibility(View.GONE);
                                holder.etNGChooseOther.setVisibility(View.GONE);
                            }
                        }

                    } else {
                        holder.rbOK.setChecked(false);
                        holder.rbNG.setChecked(false);
                        holder.tvNGChoose.setVisibility(View.GONE);
                        holder.etNGChooseOther.setVisibility(View.GONE);
                    }
                    holder.tvNGChoose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final GeneralPop titlePop = new GeneralPop(v.getContext(), popView
                                    , holder.tvNGChoose.getWidth(), DisplayUtil.dip2px(150));
                            String otherStr1 = item.getDATATYPEDETAIL() + ";其他";
                            String[] strings1 = otherStr1.split(";");
                            final List<String> list1 = Arrays.asList(strings1);
                            popDownAreaListAdapter.setNewData(list1);
                            popDownAreaListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    popDownAreaListAdapter.setCurrentSelect(position);

                                    holder.tvNGChoose.setText(list1.get(position));
                                    if (list1.get(position).equals("其他")) {
                                        item.setDATAVALUE("NG其他;");
                                        holder.etNGChooseOther.setText("");
                                        holder.etNGChooseOther.setVisibility(View.VISIBLE);
                                    } else {
                                        holder.etNGChooseOther.setVisibility(View.GONE);
                                        item.setDATAVALUE("NG;" + list1.get(position));
                                    }
                                    titlePop.dismiss();
                                   // holder.llCheck.requestFocus();  bunny 该项想改，但是不生效
                                }
                            });
                            titlePop.showAsDropDown(holder.tvNGChoose, 0, 0);
                        }
                    });
                    holder.rbOK.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            item.setDATAVALUE("OK");
                            holder.etNGChooseOther.setVisibility(View.GONE);
                            holder.tvNGChoose.setVisibility(View.GONE);
                        }
                    });
                    holder.rbNG.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            item.setDATAVALUE("NG;");
                            holder.tvNGChoose.setText("请选择");
                            holder.etNGChooseOther.setVisibility(View.GONE);
                            holder.tvNGChoose.setVisibility(View.VISIBLE);
                            holder.tvNGChoose.callOnClick(); // bunny 加入该项后，点击NG时不会乱跳焦点
                        }
                    });
                    break;
                case "3":
                    holder.etContent.setVisibility(View.GONE);
                    holder.llCheck.setVisibility(View.GONE);
                    holder.llChoose.setVisibility(View.VISIBLE);
                    if (item.getLANGUAGETYPE().equals("A")) {
                        holder.etChooseOther.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    } else {
                        holder.etChooseOther.setInputType(InputType.TYPE_CLASS_TEXT);
                    }
                    //把Bean与输入框进行绑定
                    holder.etChooseOther.setTag(item);
                    //清除焦点
                    holder.etChooseOther.clearFocus();
                    holder.etChooseOther.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //获得Edittext所在position里面的Bean，并设置数据
                            InputDataEntity.InputObject item = (InputDataEntity.InputObject) holder.etChooseOther.getTag();
                            if (item.getDATAVALUE().contains("其他;")) {
                                item.setDATAVALUE(s.length() > 0 ? "其他;" + s.toString() : "其他;");
                            } else {
                                item.setDATAVALUE(s.length() > 0 ? s.toString() : "");
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });

                    //大部分情况下，Adapter里面有if必须有else
                    if (!TextUtils.isEmpty(item.getDATAVALUE())) {
                        if (item.getDATAVALUE().contains("其他;")) {
                            holder.tvChoose.setText("其他");
                            holder.etChooseOther.setVisibility(View.VISIBLE);
                            if (!item.getDATAVALUE().equals("其他;")) {
                                holder.etChooseOther.setText(item.getDATAVALUE().split(";")[1]);
                            } else {
                                holder.etChooseOther.setText("");
                            }
                        } else {
                            holder.tvChoose.setText(item.getDATAVALUE());
                            holder.etChooseOther.setVisibility(View.GONE);
                        }
                    } else {
                        holder.tvChoose.setText("请选择");
                        holder.etChooseOther.setVisibility(View.GONE);
                    }

                    holder.tvChoose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final GeneralPop titlePop2 = new GeneralPop(v.getContext(), popView
                                    , holder.tvChoose.getWidth(), DisplayUtil.dip2px(240)); // bunny 增加下拉框长度
                            String otherStr = item.getDATADETAIL() + ";其他";
                            String[] strings = otherStr.split(";");
                            final List<String> list = Arrays.asList(strings);
                            popDownAreaListAdapter.setNewData(list);
                            popDownAreaListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    popDownAreaListAdapter.setCurrentSelect(position);
                                    popDownAreaListAdapter.notifyDataSetChanged();
                                    holder.tvChoose.setText(list.get(position));
                                    if (list.get(position).equals("其他")) {
                                        item.setDATAVALUE("其他;");
                                        holder.etChooseOther.setText("");
                                        holder.etChooseOther.setVisibility(View.VISIBLE);
                                    } else {
                                        holder.etChooseOther.setVisibility(View.GONE);
                                        item.setDATAVALUE(list.get(position));
                                    }
                                    titlePop2.dismiss();
                                }
                            });
                            titlePop2.showAsDropDown(holder.tvChoose, 0, 0);
                        }
                    });
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvTips)
        TextView tvTips;
        @BindView(R.id.llContent)
        LinearLayout llContent;
        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.etContent)
        EditText etContent;
        @BindView(R.id.llCheck)
        LinearLayout llCheck;
        @BindView(R.id.tvChoose)
        TextView tvChoose;
        @BindView(R.id.llChoose)
        LinearLayout llChoose;
        @BindView(R.id.etChooseOther)
        EditText etChooseOther;
        @BindView(R.id.etNGChooseOther)
        EditText etNGChooseOther;

        @BindView(R.id.rbOK)
        RadioButton rbOK;
        @BindView(R.id.rbNG)
        RadioButton rbNG;
        @BindView(R.id.tvNGChoose)
        TextView tvNGChoose;


        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
